package org.db.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;

import javax.swing.table.AbstractTableModel;

import org.db.dao.LageskizzenWeichenDAO;

public class LageskizzenWeichenTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6835001843144087780L;

	private List<LageskizzenWeichenDAO> data = new ArrayList<LageskizzenWeichenDAO>();

	private DecimalFormat format1 = new DecimalFormat("0.000");

	Class<?>[] types = new Class[]{java.lang.Double.class,
			java.lang.String.class, java.lang.String.class};

	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		switch (column) {
			case 0 :
				return data.get(row).getstationWeiche() == 0 ? "" : format1
						.format(data.get(row).getstationWeiche());
			case 1 :
				return data.get(row).getnummerWeiche();
			case 2 :
				return data.get(row).getbezWeiche();
			default :
				return "";

		}
	}

	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		LageskizzenWeichenDAO dao = data.get(rowIndex);
		switch (columnIndex) {
			case 0 :
				dao.setstationWeiche((Double) value);
				break;
			case 1 :
				dao.setnummerWeiche((String) value);
				break;
			case 2 :
				dao.setbezWeiche((String) value);
				break;
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	@Override
	public String getColumnName(int column) {
		switch (column) {
			case 0 :
				return "Station_Weiche";
			case 1 :
				return "WeichenNr";
			case 2 :
				return "WeichenBez";
			default :
				return "";
		}
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return types[columnIndex];
	}

	public LageskizzenWeichenDAO getElementAtRow(int row) {
		if (row < data.size()) {
			return data.get(row);
		}
		return null;
	}

	public void replaceWeichenElement(LageskizzenWeichenDAO oldDao,
			LageskizzenWeichenDAO newDao) {
		data.set(data.indexOf(oldDao), newDao);
		fireTableDataChanged();
	}

	public void removeWeichenElement(LageskizzenWeichenDAO dao) {
		data.remove(dao);
		fireTableDataChanged();
	}

	public LageskizzenWeichenDAO removeWeichenElement(int rowIndex) {
		LageskizzenWeichenDAO removedDAO = data.remove(rowIndex);
		fireTableDataChanged();
		return removedDAO;
	}

	public void addLageskizzenWeiche(LageskizzenWeichenDAO dao) {
		data.add(dao);
		Collections.sort(data);
		fireTableDataChanged();
	}

	public void addLageskizzenWeichen(SortedSet<LageskizzenWeichenDAO> daos) {
		data.addAll(daos);
		fireTableDataChanged();
	}

	public List<LageskizzenWeichenDAO> getWeichenElemente() {
		return data;
	}

	public void refresh() {
		fireTableDataChanged();
	}

	public boolean isEmpty() {
		return data.isEmpty();
	}

}
